import type { AxiosProgressEvent, GenericAbortSignal } from 'axios'
import { computed } from 'vue'
import { get, post } from '@/utils/request'
import { useSettingStore, useUserStore } from '@/store'

export function fetchChatAPI<T = any>(
  prompt: string,
  options?: { conversationId?: string; parentMessageId?: string },
  signal?: GenericAbortSignal,
) {
  return post<T>({
    url: '/chat',
    data: { prompt, options },
    signal,
  })
}

export function fetchChatConfig<T = any>() {
  return post<T>({
    url: '/config',
  })
}

export function fetchChatAPIProcess<T = any>(
  params: {
    gptType: number
    content: string
    prompt: string
    options?: { conversationId?: string; parentMessageId?: string }
    signal?: GenericAbortSignal
    onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void
  },
) {
  const userStore = useUserStore()
  const settingStore = useSettingStore()
  const userInfo = computed(() => userStore.userInfo)
  console.log(userInfo)
  return post<T>({
    // url: '/api/chat-process',
    url: userInfo.value.token ? '/v2/chat/streamCompletions' : '/v1/chat/demoStreamMsg',
    data: { prompt: params.prompt, options: params.options, gptType: params.gptType, content: params.content, systemMessage: settingStore.systemMessage },
    signal: params.signal,
    onDownloadProgress: params.onDownloadProgress,
  })
}

export function fetchSession<T>() {
  return post<T>({
    url: '/session',
  })
}

export function fetchVerify<T>(token: string) {
  return post<T>({
    url: '/verify',
    data: { token },
  })
}

export function fetchLogin<T>(data: any) {
  return post<T>({
    url: '/auth/login',
    data,
  })
}
export function fetchRegister<T>(data: any) {
  return post<T>({
    url: '/auth/register',
    data,
  })
}
export function fetchRegisterCode<T>(data: any) {
  return get<T>({
    url: '/auth/registerCode',
    data,
  })
}
export function fetchGetAccountInfo<T>(data: any) {
  return get<T>({
    url: '/auth/getAccountInfo',
    data,
  })
}

export function fetchPayPlan<T>() {
  return get<T>({
    url: '/pay/plan',
  })
}

export function fetchPayRecharge<T>(data: any) {
  return get<T>({
    url: '/pay/recharge',
    data,
  })
}
export function fetchPayGetSubscribePayUrl<T>(data: any) {
  return get<T>({
    url: '/pay/getSubscribePayUrl',
    data,
  })
}

export function fetchImagesGenerations<T>(data: any) {
  return post<T>({
    url: '/v2/images/generations',
    data,
  })
}

export function fetchPayGetResourceBundle<T>() {
  return get<T>({
    url: '/pay/resourceBundle',
  })
}
export function fetchPayGetResourceBundlePayUrl<T>(data: any) {
  return get<T>({
    url: '/pay/getResourceBundlePayUrl',
    data,
  })
}
